package com.wyh.lotterysystem.service.activitystatus.operator;

import com.wyh.lotterysystem.dao.dataobject.ActivityUserDo;
import com.wyh.lotterysystem.dao.mapper.ActivityMapper;
import com.wyh.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.wyh.lotterysystem.dao.mapper.ActivityUserMapper;
import com.wyh.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.wyh.lotterysystem.service.enums.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
public class UserOperator extends AbstractActivityOperator{

    @Autowired
    ActivityMapper activityMapper;

    @Autowired
    ActivityUserMapper activityUserMapper;

    @Autowired
    ActivityPrizeMapper activityPrizeMapper;

    @Override
    public int sequence() {
        return 1;
    }

    @Override
    public boolean convertStatus(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityUserMapper.batchUpdateStatus(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getUserId(),
                    convertActivityStatusDTO.getTargetActivityUserStatus().name());
            return true;
        }catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Integer activityId = convertActivityStatusDTO.getActivityId();
        List<Integer> userIds = convertActivityStatusDTO.getUserId();
        ActivityUserStatusEnum userStatusEnum = convertActivityStatusDTO.getTargetActivityUserStatus();

        if (null == activityId
                || CollectionUtils.isEmpty(userIds)
                || null == userStatusEnum){
            return false;
        }

        List<ActivityUserDo> activityUserDo = activityUserMapper.batchSelectByAUIds(activityId,userIds);
        if (CollectionUtils.isEmpty(activityUserDo)){
            return false;
        }

        for (ActivityUserDo activityUserDo1 : activityUserDo) {
            if (activityUserDo1.getStatus().equalsIgnoreCase(userStatusEnum.name())){
                return false;
            }
        }

        return true;
    }
}
